// packagePay/pages/book-add/book-add.js
import {
    getAIBook
} from '../../api/pay'
import {
    getBook
} from '../../../api/app'
import user from '../../../utils/user'
import {
    shake
} from '../../../utils/util'
const app = getApp()

Page({
    /**
     * 页面的初始数据
     */
    data: {
        aiBook: [],
        book: [],
        men: 0,
        women: 0,
        showAll: true,
        showAI: true,
        selectIds: [],
        companyId: '',
        projectId: '',
        pageNum: 1,
        searchText: '',
        pageLock: false,
        requestType: '',
        dataPayCallback: ''
    },
    async onLoad(options) {
        console.log(options, 'options')
        this.setData({
            companyId: options.companyId,
            projectId: options.projectId,
            requestType: options.type
        })
        this.getBook()
        // 才你想加接口
        // if(options.companyId){
        //     getAIBook({
        //         companyId: user.companyId,
        //         projectId: options.projectId,
        //         serveCompanyId: options.companyId
        //     }).then(res => {
        //         for(const i in res.list){
        //             res.list[i].name = res.list[i].userName
        //             res.list[i].userName = res.list[i].userName + `（${res.list[i].idCardNo.substr(14)}）`
        //         }
        //         console.log(res,'resres')
        //         const aiBook = this.isSelectRecovery(res.list)
        //         this.setData({
        //             aiBook
        //         })
        //     })
        // }
    },
    onUnload() {
        if (this.data.dataPayCallback) {
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.emit('dataCallback', {
                data: 'payData'
            })
            // dataCallback
        }
    },
    getBook() {
        //  1201 user.companyId
        getBook({
            companyId: user.companyId,
            pageNum: this.data.pageNum,
            pageSize: 50,
            searchText: this.data.searchText
        }).then(res => {
            if (!res.hasNextPage) {
                this.setData({
                    pageLock: true
                })
            }
            for (const i in res.list) {
                res.list[i].name = res.list[i].userName
                res.list[i].userName = res.list[i].userName + `（${res.list[i].idCardNo.substr(14)}）`
            }
            const book = this.data.book
            book.push(...this.isSelectRecovery(res.list))
            this.setData({
                book,
                men: res.men,
                women: res.women
            })
        })
    },
    // 根据selectIds恢复已选中的数据
    isSelectRecovery(list) {
        for (const i in list) {
            const ids = this.data.selectIds
            if (ids.indexOf(list[i].id) !== -1) {
                list[i].selected = true
            }
        }
        return list
    },
    onScroll() {
        if (this.data.pageLock) {
            return
        }
        this.setData({
            pageNum: this.data.pageNum + 1
        })
        this.getBook()
    },
    switchShowAll() {
        this.setData({
            showAll: !this.data.showAll
        })
    },
    switchShowAI() {
        this.setData({
            showAI: !this.data.showAI
        })
    },
    search(event) {
        if (event.detail.value)
            this.setData({
                showAI: false
            })
        shake(() => {
            this.setData({
                book: [],
                pageNum: 1,
                pageLock: false,
                searchText: event.detail.value
            })
            this.getBook()
        }, 1000)
    },
    // 选择
    switchSelect(event) {
        const workerId = event.currentTarget.dataset.id
        const index = this.data.selectIds.indexOf(workerId)
        const isSelected = index !== -1
        if (isSelected) {
            this.data.selectIds.splice(index, 1)
            this.setData({
                selectIds: this.data.selectIds
            })
        } else {
            this.data.selectIds.push(workerId)
            this.setData({
                selectIds: this.data.selectIds
            })
        }
        for (const i in this.data.book) {
            if (workerId === this.data.book[i].id) {
                this.setData({
                    ['book[' + i + '].selected']: !isSelected
                })
                break
            }
        }
        for (const i in this.data.aiBook) {
            if (workerId === this.data.aiBook[i].id) {
                this.setData({
                    ['aiBook[' + i + '].selected']: !isSelected
                })
                break
            }
        }
    },
    onReportAiClick() {
        wx.reportEvent('guess_add')
    },
    // 提交添加
    complete() {
        if (!this.data.selectIds.length) {
            app.toast('请至少选择一人加入项目')
            return 0
        }
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.emit('acceptDataFromOpenedPage', {
            data: this.data.selectIds
        })
        setTimeout(() => {
            wx.navigateBack()
        }, 1000)

        // addToProjectFromBook({
        //     projectId: this.data.projectId,
        //     serveCompanyId: this.data.companyId,
        //     addressBookIds: this.data.selectIds
        // }).then(()=>{
        //     app.toast('添加成功')
        //     setTimeout(()=>{
        //         wx.navigateBack()
        //     },1000)
        // }) 
    },
    onGoProjectImport() {
        console.log('点击啦')
        wx.navigateTo({
            url: '/packageInsurance/pages/project-list-import/project-list-import?id=' + this.data.projectId + '&type=' + (this.data.requestType == 'pay' ? 'pay' : this.data.requestType === 'eleSign' ? 'eleSign' : ''),
            events: {
                dataCallback: (data) => {
                    this.setData({
                        dataPayCallback: data.data
                    })
                    console.log(data, '页面b回来的数据')
                },
            }
        })
    }
})